package cosplay.Controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import cosplay.Beans.CompetitionDetails;
import cosplay.Beans.Photos;
import cosplay.DAO.CompetitionDAOImpl;
import cosplay.DAO.PhotoDAOImpl;
import cosplay.DAO.CompetitionDetailDAOImpl;
import cosplay.DAO.UserDAOImpl;
import cosplay.Services.CommonServices;

@Controller
public class CompetitionDetailController extends MultiActionController{
	@Autowired
	CompetitionDetailDAOImpl detaildao;
	@Autowired
	CompetitionDAOImpl competitiondao;
	@Autowired
	PhotoDAOImpl photodao;
	@Autowired
	UserDAOImpl userdao;
	
	@RequestMapping(value="/userjoin.htm",method=RequestMethod.GET)
	protected ModelAndView handelShowJoin (HttpServletRequest req, HttpServletResponse res) throws Exception
	{
		ModelMap model= new ModelMap();
		model.addAttribute("target","userjoin.htm");
		model.addAttribute("message","Please Enter Information");
		return new ModelAndView("detailmodify","model",model);
	}
	
	@RequestMapping(value="/userjoin.htm",method=RequestMethod.POST)
	protected ModelAndView handelActJoin(HttpServletRequest req, HttpServletResponse res) throws Exception
	{
		CompetitionDetails detail=getDetailFromRequest(req);
		ModelMap model=new ModelMap();
		if (detail!=null)
		{
			if(userdao.join(detail))
			{
				model.addAttribute("message","Add Successed");
				model.addAttribute("detail",detail);
				model.addAttribute("target","editdetail.htm");
			}
			else
			{
				model.addAttribute("message","AddSuccessed");
				model.addAttribute("detail",detail);
				model.addAttribute("target","userjoin.htm");
			}
		}
		else
		{
			model.addAttribute("message","Input Error");
			model.addAttribute("target","userjoin.htm");
		}
		return new ModelAndView("detailmodify","model",model);
	}
	
	@RequestMapping(value="/editdetail.htm",method=RequestMethod.GET)
	protected ModelAndView handelShowEdit(HttpServletRequest req, HttpServletResponse res)throws Exception
	{
		int userid=userdao.getIDByName(CommonServices.getLoggedUsername(req));
		int competitionid=competitiondao.getcurrent().getId();
		CompetitionDetails detail=detaildao.get(userid, competitionid);
		ModelMap model=new ModelMap();
		if (detail!=null)
		{
			model.addAttribute("detail",detail);
		}
		model.addAttribute("message","Please input new infromation");
		model.addAttribute("target","editdetail");
		return new ModelAndView("detailmodify","model",model);
	}
	
	@RequestMapping(value="/editdetail.htm",method=RequestMethod.POST)
	protected ModelAndView handelActEdit(HttpServletRequest req, HttpServletResponse res)throws Exception
	{
		int userid=userdao.getIDByName(CommonServices.getLoggedUsername(req));
		int competitionid=competitiondao.getcurrent().getId();
		CompetitionDetails detail=getDetailFromRequest(req);
		ModelMap model=new ModelMap();
		if(detaildao.edit(userid, competitionid, detail))
		{
			model.addAttribute("message","update successed");
			model.addAttribute("detail",detaildao.get(userid, competitionid));
			model.addAttribute("target","editdetail.htm");
		}
		else
		{
			model.addAttribute("message","update failed");
			model.addAttribute("detail",detail);
			model.addAttribute("target","editdetail.htm");
		}
		return new ModelAndView("detailmodify","model",model);
	}
	
	@RequestMapping(value="userquit.htm")
	protected ModelAndView handelActQuit(HttpServletRequest req, HttpServletResponse res)throws Exception
	{
		int userid=userdao.getIDByName(CommonServices.getLoggedUsername(req));
		int competitionid=competitiondao.getcurrent().getId();
		if(userdao.quit(userid, competitionid))
		{
			res.setIntHeader("sucess", 1);
		}
		else
		{
			res.setIntHeader("sucess", 0);
		}
		return null;
	}
	
	@RequestMapping(value="/showdetail.htm")
	protected ModelAndView handelShowResult(HttpServletRequest req, HttpServletResponse res) throws Exception
	{
		int rid=CommonServices.getIDFromRequest(req, "detailid");
		CompetitionDetails detail=detaildao.get(rid);
		List<Photos> photos =new ArrayList<Photos>();
		for (Integer pid:photodao.getByResult(rid))
		{
			photos.add(photodao.get(pid));
		}
		ModelMap model=new ModelMap();
		model.addAttribute("detail",detail);
		model.addAttribute("photo", photos);
		model.addAttribute("photocount",photos.size());
		return new ModelAndView("showdetailsingle","model",model);
	}

	@RequestMapping(value="/showdetaillist.htm")
	protected ModelAndView handleShowResultList(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		// TODO Auto-generated method stub
		ModelMap model=new ModelMap();
		model.addAttribute("message",CommonServices.getMessageFromRequest(req));
		model.addAttribute("target",req.getParameter("target"));
		model.addAttribute("page",CommonServices.getPageFromRequest(req));
		return new ModelAndView("showdetails","model",model);
	}
	
	@RequestMapping(value="/latestphotoshots.htm")
	protected ModelAndView handleShowLastJoinedContent(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		// TODO Auto-generated method stub
		int cid=CommonServices.getIDFromRequest(req, "competitionid");
		if (cid<1)
		{
			cid=competitiondao.getcurrent().getId();
		}
		List<CompetitionDetails> details=detaildao.getLastJoined(cid);
		int resultcount=details.size();
		ModelMap model=new ModelMap();
		model.addAttribute("detail",details);
		model.addAttribute("resultcount", resultcount);
		return new ModelAndView("detaillist","model",model);
	}
	
	@RequestMapping(value="/mostviews.htm")
	protected ModelAndView handleShowMostViewedContent(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		// TODO Auto-generated method stub
		int cid=CommonServices.getIDFromRequest(req, "competitionid");
		if (cid<1)
		{
			cid=competitiondao.getcurrent().getId();
		}
		List<CompetitionDetails> details=detaildao.getMostViewed(cid);
		int resultcount=details.size();
		ModelMap model=new ModelMap();
		model.addAttribute("detail",details);
		model.addAttribute("resultcount", resultcount);
		return new ModelAndView("detaillist","model",model);
	}
	
	@RequestMapping(value="/mostvotes.htm")
	protected ModelAndView handleShowMostVotedContent(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		// TODO Auto-generated method stub
		int cid=CommonServices.getIDFromRequest(req, "competitionid");
		if (cid<1)
		{
			cid=competitiondao.getcurrent().getId();
		}
		List<CompetitionDetails> details=detaildao.getMostVoted(cid);
		int resultcount=details.size();
		ModelMap model=new ModelMap();
		model.addAttribute("detail",details);
		model.addAttribute("resultcount", resultcount);
		return new ModelAndView("detaillist","model",model);
	}
	
	@RequestMapping(value="/mostcomments.htm")
	protected ModelAndView handleShowMostCommentedContent(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		// TODO Auto-generated method stub
		int cid=CommonServices.getIDFromRequest(req, "competitionid");
		if (cid<1)
		{
			cid=competitiondao.getcurrent().getId();
		}
		List<CompetitionDetails> details=detaildao.getMostComment(cid);
		int resultcount=details.size();
		ModelMap model=new ModelMap();
		model.addAttribute("detail",details);
		model.addAttribute("resultcount", resultcount);
		return new ModelAndView("detaillist","model",model);
	}
	
	@RequestMapping(value="/allphotoshots.htm")
	protected ModelAndView handleShowCompetitionResultContent(HttpServletRequest req,
			HttpServletResponse res) throws Exception {
		// TODO Auto-generated method stub
		int cid=CommonServices.getIDFromRequest(req, "competitionid");
		if (cid==0)
			cid=competitiondao.getcurrent().getId();
		List<CompetitionDetails> details=detaildao.getAll(cid);
		int resultcount=details.size();
		ModelMap model=new ModelMap();
		model.addAttribute("detail",details);
		model.addAttribute("resultcount", resultcount);
		return new ModelAndView("detaillist","model",model);
	}
	
	private CompetitionDetails getDetailFromRequest(HttpServletRequest req)
	{
		if (CommonServices.getLoggedUsername(req)==null)
		{
			return null;
		}
		if ((!req.getParameterMap().containsKey("character"))||(req.getParameter("character").isEmpty()))
			return null;
		if ((!req.getParameterMap().containsKey("costume"))||(req.getParameter("costume").isEmpty()))
			return null;
		if ((!req.getParameterMap().containsKey("photographer"))||(req.getParameter("photographer").isEmpty()))
			return null;
		if ((!req.getParameterMap().containsKey("type"))||(req.getParameter("type").isEmpty()))
			return null;
		CompetitionDetails detail=new CompetitionDetails();
		detail.setCharacter(req.getParameter("character"));
		detail.setCostume(req.getParameter("costume"));
		detail.setDescription(req.getParameter("description"));
		detail.setPhotographer(req.getParameter("photographer"));
		detail.setType(req.getParameter("type"));
		detail.setUserID(userdao.getIDByName(CommonServices.getLoggedUsername(req)));
		detail.setCompetitionID(competitiondao.getcurrent().getId());
		detail.setId(CommonServices.getIDFromRequest(req, "detailid"));
		return detail;
	}

}
